﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace SystemDiagramNew.TypeDefinition
{
	public class PipingSystemInfo
	{
		public void Transform(Transform transform)
		{
			foreach (Node node in this.m_NodeList)
			{
				node.Transform(transform);
			}
			List<PipeInfo> list = this.PipeInfoList();
			foreach (PipeInfo pipeInfo in list)
			{
				pipeInfo.TransformSplitPoints(transform);
			}
		}

		public void Transform(IEnumerable<LinkedDocument> linkedDocuments)
		{
			if (linkedDocuments == null || !linkedDocuments.Any<LinkedDocument>())
			{
				return;
			}
			this.m_NodeList.ForEach(delegate(Node node)
			{
				LinkedDocument linkedDocument = linkedDocuments.FirstOrDefault((LinkedDocument ld) => ld.Document.Contains(node.Component.Id));
				if (linkedDocument != null)
				{
					node.Transform(linkedDocument.Transform);
				}
			});
		}

		public void TransformShaftSide(double angle)
		{
			foreach (Node node in this.m_NodeList)
			{
				node.TransformShaftSide(angle);
			}
			List<PipeInfo> list = this.PipeInfoList();
			foreach (PipeInfo pipeInfo in list)
			{
				pipeInfo.TransformSplitPointsToShaftSide(angle);
			}
		}

		public List<PipeInfo> PipeInfoList()
		{
			List<PipeInfo> list = new List<PipeInfo>();
			foreach (Node node in this.m_NodeList)
			{
				List<PipeInfo> pipeList = node.PipeList;
				for (int i = 0; i < pipeList.Count; i++)
				{
					PipeInfo pipe = pipeList[i];
					int num = list.FindIndex((PipeInfo s) => s.IsEqual(pipe));
					if (num == -1)
					{
						list.Add(pipeList[i]);
					}
				}
			}
			return list;
		}

		public void RomoveNode(Node node)
		{
			int num = this.m_NodeList.FindIndex((Node s) => s.IsEqual(node));
			if (num != -1)
			{
				this.m_NodeList.RemoveAt(num);
			}
		}

		private void Sort()
		{
			this.m_NodeList.Sort(new NodePositionComparer());
		}

		public bool FindNodeByPosition(XYZ pos, ref Node node)
		{
			int num = this.m_NodeList.FindIndex((Node s) => s.Position.IsAlmostEqualTo(pos, 0.0001));
			if (num > -1)
			{
				node = this.m_NodeList[num];
				return true;
			}
			return false;
		}

		private Node FindNodeByPosition(XYZ pos, List<Node> nodeList, List<PipeInfo> nodePoleList)
		{
			int num = nodeList.FindIndex((Node s) => s.Position.IsAlmostEqualTo(pos, 0.0001));
			if (num > -1)
			{
				Node node = nodeList[num];
				foreach (PipeInfo pipe in nodePoleList)
				{
					node.AddPipe(pipe);
				}
				return node;
			}
			return new Node(Node.NodeType.NT_NULL, null, pos, nodePoleList);
		}

		public ForceType ForceType { get; set; }

		public string SysName
		{
			get
			{
				return this.m_SysName;
			}
		}

		public List<Node> NodeList
		{
			get
			{
				return this.m_NodeList;
			}
		}

		public bool IsNull
		{
			get
			{
				return this.m_NodeList == null || this.m_NodeList.Count < 1;
			}
		}

		public PipingSystemInfo(PipingSystemInfo pipingSysInfo)
		{
			this.m_SysName = pipingSysInfo.m_SysName;
			this.m_NodeList = pipingSysInfo.m_NodeList;
			this.ForceType = pipingSysInfo.ForceType;
		}

		public PipingSystemInfo(string sysName, List<Node> nodeList, ForceType ft = ForceType.Pressure)
		{
			this.m_SysName = sysName;
			this.m_NodeList = nodeList;
			this.ForceType = ft;
			this.Sort();
		}

		private readonly string m_SysName = "";

		private readonly List<Node> m_NodeList = new List<Node>();
	}
}
